package com.xyx.yfy_site_msg.system.controller;


import cn.hutool.captcha.CaptchaUtil;
import cn.hutool.captcha.CircleCaptcha;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.xyx.yfy_site_msg.system.entity.YfyLoginfo;
import com.xyx.yfy_site_msg.system.entity.YfyUser;
import com.xyx.yfy_site_msg.system.service.YfyLoginfoService;
import com.xyx.yfy_site_msg.system.service.YfyUserService;
import com.xyx.yfy_site_msg.system.utils.ActiverUser;
import com.xyx.yfy_site_msg.system.utils.ResultObj;
import com.xyx.yfy_site_msg.system.utils.WebUtils;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import sun.misc.BASE64Encoder;
import sun.security.provider.MD4;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.Date;

/**
 * <p>
 * 登录控制器
 * </p>
 *
 * @author xyx
 * @since 2020-11-09
 */
@Controller
@RequestMapping("/login")
public class YfyLoginController {

    @Autowired
    private YfyUserService userService;
    @Autowired
    private YfyLoginfoService loginfoService;

    /*
     * 跳转到登录页面
     * */
    @RequestMapping("toLogin")
    public String toLogin() {
        return "login";
    }

    /*
     * 登录控制器
     */
    @RequestMapping("login")
    @ResponseBody
    public ResultObj login(String username, String password, HttpSession session) {
        Subject subject = SecurityUtils.getSubject();
        UsernamePasswordToken token = new UsernamePasswordToken(username, password);
        try {
            subject.login(token);
            ActiverUser activerUser = (ActiverUser) subject.getPrincipal();
            WebUtils.getSession().setAttribute("user", activerUser.getUser());
            YfyLoginfo yfyLoginfo = new YfyLoginfo();
            yfyLoginfo.setLLoginname(username);
            yfyLoginfo.setLLoginip(WebUtils.getRequest().getRemoteAddr());
            yfyLoginfo.setLLogintime(new Date());
            this.loginfoService.save(yfyLoginfo);
            return new ResultObj(200, "登录成功");
        } catch (AuthenticationException e) {
            e.printStackTrace();
            return new ResultObj(-1, "用户名或密码错误，请重新输入");
        }
    }
//    /*
//     * 登录控制器
//     */
//    @RequestMapping("login")
//    @ResponseBody
//    public ResultObj login(String username, String password, String code, HttpSession session) {
//        //从session中拿到正确的code，进行比对
//        Object code_session = session.getAttribute("code");
//        if (code != null && code.equals(code_session)) {
//            Subject subject = SecurityUtils.getSubject();
//            UsernamePasswordToken token = new UsernamePasswordToken(username, password);
//            try {
//                subject.login(token);
//                ActiverUser activerUser = (ActiverUser) subject.getPrincipal();
//                WebUtils.getSession().setAttribute("user", activerUser.getUser());
//                YfyLoginfo yfyLoginfo = new YfyLoginfo();
//                yfyLoginfo.setLLoginname(username);
//                yfyLoginfo.setLLoginip(WebUtils.getRequest().getRemoteAddr());
//                yfyLoginfo.setLLogintime(new Date());
//                this.loginfoService.save(yfyLoginfo);
//                return new ResultObj(200, "登录成功");
//            } catch (AuthenticationException e) {
//                e.printStackTrace();
//                return new ResultObj(-1, "用户名或密码错误，请重新输入");
//            }
//        }
//        return new ResultObj(-1, "验证码有误，请重新输入");
//    }


//    /*
//     * 生成验证码并返回验证码图片
//     * */
//    @RequestMapping("getCode")
//    public void getCode(HttpServletResponse response, HttpSession session) throws IOException {
//        //定义图形验证码的长、宽、验证码字符数、干扰元素个数
//        CircleCaptcha captcha = CaptchaUtil.createCircleCaptcha(116, 36, 4, 2);
//        //得到code
//        String code = captcha.getCode();
//        //放入session
//        session.setAttribute("code", code);
////        System.out.println(code);//控制台打印验证码
//        //写入到流并返回
//        ServletOutputStream outputStream = response.getOutputStream();
//        //CircleCaptcha captcha = new CircleCaptcha(200, 100, 4, 20);
//        //图形验证码写出，可以写出到文件，也可以写出到流
//        captcha.write(outputStream);
//        outputStream.close();
//    }
}
